import $service from '../../../../service'
const app = getApp()
let lianjiangInquiryListEndDate = '';
let lianjiangInquiryListStartDate = ""
let healthCardNo = ''
let patientId = ''
let page = 1 
let loading = true

Page({

  data: {
    list: []
  },
  onLoad(e) {
    healthCardNo = e.healthCardNo
    patientId = e.patientId
    wx.setNavigationBarTitle({
      title: e.patientName + '的病历',
    })
    lianjiangInquiryListEndDate = ''
    lianjiangInquiryListStartDate = ""
    page = 1
    loading = true
    this.getClinicHistoryList()
  },
  onReachBottom() {
    if (loading) {
      page ++ 
      this.getClinicHistoryList()
    }
  },
  // 获取廉江his就诊记录
  getClinicHistoryList() {
    let params = {
      "cardType": "03",
      "healthCardNo": healthCardNo,
      "patientId": patientId,
      "current": page,
      "size": 10,
      // "healthCardNo": 900663823,
      // "patientId": "DAF479145F2126E6E050A8C0010004EA",
    }
    if (lianjiangInquiryListEndDate && lianjiangInquiryListStartDate) {
      params.beginDate = lianjiangInquiryListStartDate
      params.endDate = lianjiangInquiryListEndDate
    }
    app.http.getClinicHistoryList(params).then(res => {
      if (res.data && res.data.count > this.data.list.length) {
        let list = [...this.data.list, ...res.data.clinicHistory]
        this.setData({
          list
        })
      } else {
        loading = false
      }
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  toDetail(e) {
    $service.openPage(`../liangjiang-inquiry-history-detail/liangjiang-inquiry-history-detail?id=${e.detail}`)
  },
  lianjiangHisChangeDate(e) {
    let {endDate, startDate} = e.detail
    if (endDate && startDate) {
      loading = true
      lianjiangInquiryListStartDate = startDate
      lianjiangInquiryListEndDate = endDate
      page = 1
      this.setData({
        list: []
      })
      this.getClinicHistoryList()
    }
  },
})